// 获取当前选项卡ID
function getCurrentTabId(callback) {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        if (callback) callback(tabs.length ? tabs[0].id : null);
    });
}

function updateTab(tab) {
    if (/^https?:\/\/imgur\.com\/.*\.(png|jpg|gif)$/.test(tab.url))
        chrome.tabs.update(tab.id, {
            url: 'https://img.noobzone.ru/getimg.php?url=' + tab.url
        })
}

chrome.webRequest.onBeforeRequest.addListener(
    function (details) {
        var url = details.url;

        if (/^https?:\/\/(i\.)?imgur\.com\/.*\.(png|jpg|gif).*$/.test(url))
            //  return { redirectUrl: 'https://images.weserv.nl/?url=' + url };
            // return { redirectUrl: 'https://img.noobzone.ru/getimg.php?url=' + url };
        return { redirectUrl: 'https://pic1.xuehuaimg.com/proxy/' + url.replace(/^https?:\/\//, '') }
        if (url === 'https://www.chess.com/bundles/web/images/lessons/coachdavid.png')
            return { redirectUrl: 'http://pic.baike.soso.com/p/20131203/20131203171021-1597006920.jpg' }
        if (url === 'https://images.chesscomfiles.com/uploads/v1/user/66745986.abe8c2af.200x200o.d31169795c59.png')
            return { redirectUrl: 'https://ts1.cn.mm.bing.net/th/id/R-C.ed43cc9953f1c2ec358cf174d79c2832?rik=jLBS24OmF6MPSw&riu=http%3a%2f%2fimg.crcz.com%2fallimg%2f202001%2f07%2f1578387974124581.jpg&ehk=Us1c0joG%2fMS1YcpdACDZezHa%2bwssTDbFRbGU5%2b7iH%2bc%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1' }

        return true;

    },
    { urls: ["<all_urls>"] }, ["blocking"]
)
/* 
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    //updateTab(tab)
});


chrome.webNavigation.onBeforeNavigate.addListener(function (details) {
    alert('BeforeNavigate')
    alert(JSON.stringify(details))
});




chrome.webNavigation.onCommitted.addListener(function (e) {
    //alert('committed')
});


chrome.browserAction.onClicked.addListener(function (activeTab) {

    //chrome.tabs.executeScript(activeTab.id, { code: 'document.body.style.backgroundColor="red"' });
    updateTab(activeTab)

    //chrome.tabs.create({ url: 'https://img.noobzone.ru/getimg.php?url=' + activeTab.url });
});


 */
